﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  CustomerManage.aspx.cs
 作    者:范徐超
 编写日期:2009-11-10
 审    核:
 审核日期:
 描    述:员工列表
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text.RegularExpressions;
using System.IO;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;

public partial class Business_Customer_CustomerManage : WebUI.Page.BasePage
{
    private CustomersBO cusBO = new CustomersBO();
    private CustomersMO cusMO = new CustomersMO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvitemBind();
        }
    }

    #region 全选事件 protected void All_check(object sender, EventArgs e)
    /// <summary>
    /// 全选按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void All_check(object sender, EventArgs e)
    {
        rebackLookout();//清空查询框，后续补全
        for (int i = 0; i < gvitem.Rows.Count; i++)
        {
            ((CheckBox)gvitem.Rows[i].FindControl("chkSelect")).Checked = this.ChkAll.Checked;
        }
    }
    #endregion

    #region 删除事件 protected void butDelete_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 删除Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butDelete_Click(object sender, ImageClickEventArgs e)
    {

        rebackLookout();//清空查询框
        bool Mgtest = false;//判断是否有选择
        for (int i = 0; i < this.gvitem.Rows.Count; i++)
        {
            bool isChecked = ((CheckBox)gvitem.Rows[i].FindControl("chkSelect")).Checked;
            if (isChecked)//判断是否选择
            {
                Mgtest = true;
                int intID = Convert.ToInt32(((Label)gvitem.Rows[i].FindControl("LblID")).Text);
                try
                {
                    cusBO.Delete(intID);
                }
                catch (Exception ex)
                {
                    string errortip = "记录ID：" + intID.ToString() + "删除出现异常，请通知管理员，以下为出错提示信息：" + ex.Message.ToString();
                    PageError(errortip, "CustomerManage.aspx", "返回操作", "");
                }
            }
        }

        //没有选择项时的提示
        if (!Mgtest)
        {
            //重新绑定显示
            gvitemBind();
            ChkAll.Checked = false;
            PageError("请选择要删除的记录！", "CustomerManage.aspx", "返回操作", "");
        }/* end of if()*/
        else
        {
            //重新绑定显示
            gvitemBind();
            ChkAll.Checked = false;
            PageRight("恭喜，删除操作成功", "CustomerManage.aspx", "返回继续操作", "");
        }
    }
    #endregion

    #region 显示产品列表 private void gvitemBind()
    /// <summary>
    /// 显示产品列表
    /// </summary>
    private void gvitemBind()
    {
        DataSet ds = new DataSet();
        string strKeyContent = this.txtPropasal.Text.Trim().ToString();
        ds = string.IsNullOrEmpty(txtPropasal.Text.Trim())?cusBO.GetAllList():cusBO.GetList(" CustomerName like '%" + txtPropasal.Text + "%' or CustomerMobile like '%" + txtPropasal.Text + "%' ");


        DataView dvforalldata = new DataView();
        ViewState["numbers"] = ds.Tables[0].Rows.Count.ToString();
        dvforalldata.Table = ds.Tables[0];
        dvforalldata.Sort = "CustomerID asc";
        gvitem.DataSource = dvforalldata;

        gvitem.DataBind();
        butState();   //显示分页按钮状态
        ShowStats(); //显示页数
    }

    #endregion

    #region 翻页事件 protected void PagerButtonClick(object sender, EventArgs e)
    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "next":
                if (gvitem.PageIndex < (gvitem.PageCount - 1))
                {
                    gvitem.PageIndex += 1;
                }
                break;
            case "prev":
                if (gvitem.PageIndex > 0)
                {
                    gvitem.PageIndex -= 1;
                }
                break;
            case "last":
                gvitem.PageIndex = (gvitem.PageCount - 1);
                break;
            default:
                gvitem.PageIndex = System.Convert.ToInt32(arg);
                break;
        }/* end of  switch (arg) */
        gvitemBind();

    }
    #endregion

    #region 翻页按钮状态 private void butState()
    /// <summary>
    /// 翻页按钮状态
    /// </summary>
    private void butState()
    {
        if (this.gvitem.PageIndex < 1)
        {
            this.btnFirst.Enabled = false;
            this.btnPrev.Enabled = false;
        }
        else
        {
            this.btnFirst.Enabled = true;
            this.btnPrev.Enabled = true;
        }

        if ((this.gvitem.PageIndex + 2) > this.gvitem.PageCount)
        {
            this.btnLast.Enabled = false;
            this.btnNext.Enabled = false;
        }
        else
        {
            this.btnLast.Enabled = true;
            this.btnNext.Enabled = true;
        }
    }
    #endregion

    #region 显示页数 private void ShowStats()
    /// <summary>
    /// 显示页数
    /// </summary>
    private void ShowStats()
    {
        lblCurrentIndex.Text = "第 <font color='red'>" + (this.gvitem.PageIndex + 1).ToString() + "</font> 页";
        string Pagecount = "";
        int i = int.Parse(this.gvitem.PageCount.ToString());
        if (i == 0)
        {
            Pagecount = "1";
        }
        else
        {
            Pagecount = this.gvitem.PageCount.ToString();
        }
        lblNumbers.Text = "总共 " + ViewState["numbers"].ToString() + " 个";
        lblPageCount.Text = "共 <font color='red'>" + Pagecount + "</font> 页";
    }
    #endregion

    #region gridview中的事件

    #region RowDataBound事件 protected void gvitem_RowDataBound(object sender, GridViewRowEventArgs e)
    /// <summary>
    /// RowDataBound事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvitem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //ID绑定
            Label LblID = (Label)e.Row.FindControl("LblID");
        }

        //鼠标在列表行中停留时的样式
        for (int i = -1; i < gvitem.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
    #endregion

    #region PageIndexChanging事件 protected void gvitem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    /// <summary>
    /// PageIndexChanging事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvitem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvitem.PageIndex = e.NewPageIndex;
    }
    #endregion

    #endregion

    #region  查询事件 protected void butlookout_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 查询事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butlookout_Click(object sender, ImageClickEventArgs e)
    {

        gvitemBind();
    }
    #endregion

    #region 清空查询后的状态 private void rebackLookout()
    /// <summary>
    /// 清空查询后的状态
    /// </summary>
    private void rebackLookout()
    {
        txtPropasal.Text = "";
    }
    #endregion

    #region 添加经销商 protected void ibtnxinzeng_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 添加经销商
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnxinzeng_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("CustomerAdd.aspx");
    }
    #endregion
}
